<template>
  <el-dialog v-model="isShowDialog"
             :before-close="handleClose"
             :title="title"
             draggable
             width="500px"
             append-to-body
  >
    <el-form :model="saveParams" ref="formRef" label-width="100px" :rules="rules" :disabled="isView">

      <el-form-item label="仓库编码" prop="warehouseCode">
        <el-input v-model="saveParams.warehouseCode" @input="saveParams.warehouseCode=limitCharAndNumber(saveParams.warehouseCode)" :disabled="isView" placeholder="仓库编码" clearable/>
      </el-form-item>
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input v-model="saveParams.warehouseName" placeholder="仓库名称" clearable/>
      </el-form-item>
      <el-form-item label="收货人" prop="receiptByName">
        <el-input v-model="saveParams.receiptByName" placeholder="收货人" clearable/>
      </el-form-item>
      <el-form-item label="联系方式" prop="receiptPhone">
        <el-input v-model="saveParams.receiptPhone" @input="saveParams.receiptPhone=limitIntNumber(saveParams.receiptPhone)" :maxlength="11" placeholder="联系方式" clearable/>
      </el-form-item>
      <el-form-item label="收货地址" prop="receiptAddress">
        <el-input v-model="saveParams.receiptAddress" placeholder="收货地址" clearable/>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="saveParams.remark" placeholder="备注" clearable/>
      </el-form-item>
      <el-form-item label="状态">
        <el-select
            style="width: 100%;"
            clearable
            placeholder="状态"
            v-model="saveParams.status"
        >
          <el-option label="启用" :value="0" ></el-option>
          <el-option label="停用" :value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="clickSave" v-if="!isView"> 保存 </el-button>
          <el-button @click="handleClose">取消</el-button>
        </span>
    </template>

  </el-dialog>
</template>

<script setup>
import {warehouseSave} from "@/api/scApi";
import {getCurrentInstance, onMounted, ref} from "vue";
import {limitCharAndNumber} from "@/utils/shuangcheng";
import {getCompanySelect} from "@/api/system/user";

const {proxy} = getCurrentInstance();
const props = defineProps({
  isShowDialog: {
    type: Boolean,
    default: false
  },
  title: {type: String},
  rowData:{type:Object},
  isView:{type:Boolean}
})
const emits = defineEmits(['cancel'])

function handleClose() {
  emits('cancel')
}

let saveParams = ref(JSON.parse(JSON.stringify(props.rowData)))
 onMounted(()=>{
   if (!saveParams.value.status) {
     saveParams.value.status =0
   }

 })
// 表单校验
const rules = ref({
  warehouseCode: [{required: true, message: "仓库编码不能为空", trigger: "blur"}],
  warehouseName: [{required: true, message: "仓库名称不能为空", trigger: "blur"}],

  receiptByName: [
    {required: true, message: "收货人不能为空", trigger: "blur"}
  ],
  receiptPhone: [
    {required: true, message: "联系方式不能为空", trigger: "blur"},
    {
      pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
      message: "请输入正确的手机号码",
      trigger: "blur",
    }
  ],
  receiptAddress: [{required: true, message: "收货地址不能为空", trigger: "blur"}],
});

const sysUserInfo=JSON.parse(localStorage.getItem("userInfo"))
function clickSave() {

  proxy.$refs.formRef.validate((valid, fileds) => {
    if (valid) {

      warehouseSave(saveParams.value).then(res => {
        if (res.code === 200) {
          proxy.$modal.msgSuccess('保存成功')
          emits('cancel', true)
        }
      })
    }
  })
}


</script>

<style scoped lang="scss">

</style>